package com.code;

import java.util.ArrayList;

import com.database.DBCallHistory;
import com.example.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MessageOption extends Activity {
	private ListView lvOption;
	private String valueItem = "";
	private DBCallHistory dbCHistory;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Aut o-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.optiontext);
		this.setTitle("Message Option");

		ArrayList<String> valueOption = new ArrayList<String>();
		valueOption.add(0, "Copy");
		valueOption.add(1, "Delete");
		valueOption.add(2, "Delete All");

		lvOption = (ListView) findViewById(R.id.lvOption);
		valueItem = super.getIntent().getExtras().getString("valueItem");
		ArrayAdapter<String> arrAp = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, valueOption);
		lvOption.setAdapter(arrAp);
		dbCHistory = new DBCallHistory(this);

		lvOption.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View v, int index,
					long arg3) {
				// TODO Auto-generated method stub
				String value = lvOption.getItemAtPosition(index).toString();
				if (value.equals("Copy")) {
					ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
					clipboard.setText(valueItem);

				} else if (value.equals("Delete")) {
					String idDetail = "";
					for (int i = 0; i < valueItem.length(); i++) {
						if (valueItem.charAt(i) != '.') {
							idDetail = idDetail + valueItem.charAt(i);
						} else
							break;
					}
					if (!idDetail.equals("")) {
						dbCHistory.open();
						dbCHistory.DeleteCallHistoryDetailByID(idDetail);
						dbCHistory.close();
						setResult(RESULT_OK);
					}
				} else if (value.equals("Delete All")) {
					dbCHistory.open();
					dbCHistory.DeleteCallHistoryDetail();
					dbCHistory.DeleteCallHistory();
					dbCHistory.close();
					setResult(RESULT_OK);
				}
				finish();
			}
		});

	}
}
